package djboom.materials.display.elements {
	import djboom.materials.events.SampleSelectableEvent;
	import djboom.materials.state.Font;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 28.04.2010
	 * 
	 */

	public class SampleSelectable extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 22, 0x676767, null, true, null, null, null, "center");

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _sampleName:String;
		private var _clicked:Boolean = false;

		private var _bitmapSample:CasaBitmap;
		private var _rhombusShape:Rhombus;
		private var _titleField:CasaTextField;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function SampleSelectable(sampleName:String, bitmapData:BitmapData) {
			super();

			_sampleName = sampleName;

			this.buttonMode = true;			
			this.useHandCursor = true;
			
			_bitmapSample = new CasaBitmap(bitmapData);
			_bitmapSample.x = 40;
			addChild(_bitmapSample);
			
			_rhombusShape = new Rhombus(Rhombus.GRAY);
			_rhombusShape.x = 40;
			addChild(_rhombusShape);
			
			_titleField = new CasaTextField();
			_titleField.embedFonts = true;
			_titleField.selectable = false;			_titleField.multiline = true;			_titleField.wordWrap = true;
			_titleField.defaultTextFormat = TITLE_TFORMAT;
			_titleField.autoSize = TextFieldAutoSize.CENTER;
			_titleField.antiAliasType = AntiAliasType.ADVANCED;
			_titleField.text = sampleName;
			_titleField.mouseEnabled = false;
			_titleField.thickness = 50;
			_titleField.width = 200;
			_titleField.y = 110;			addChild(_titleField);
			
			addEventListener(MouseEvent.CLICK, clickSample);
            
			addEventListener(Event.REMOVED_FROM_STAGE, clearPage);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearPage(event:Event):void {
			_bitmapSample = null;			_rhombusShape = null;			_titleField = null;
		}

		private function clickSample(event:MouseEvent):void {
			_clicked = !_clicked;
 
			var newTextFormat:TextFormat = new TextFormat();
			
			var sampleSelectableEvent:SampleSelectableEvent;
            
			if (_clicked) {
				newTextFormat.bold = true;
				newTextFormat.color = 0x709402;
				
				
				_rhombusShape.changeColor(Rhombus.GREEN);
				
				sampleSelectableEvent = new SampleSelectableEvent(SampleSelectableEvent.SELECT);
			} else {
				newTextFormat.bold = false;
				newTextFormat.color = 0x676767;
				
				_rhombusShape.changeColor(Rhombus.GRAY);
				
				sampleSelectableEvent = new SampleSelectableEvent(SampleSelectableEvent.DESELECT);
			}
			
			sampleSelectableEvent.sampleName = _sampleName;
			dispatchEvent(sampleSelectableEvent);
			
			_titleField.setTextFormat(newTextFormat);
		}
		
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------
		
		// ----------------------------------------------------------------------------
        // Публичные свойства.
        // ----------------------------------------------------------------------------
	}
}
